package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private MovieRepository movieRepository;

    /**
     * 电影列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        List<Movie> list = movieRepository.findAll();
        model.addAttribute("list", list);
        return "movie/index";
    }

    /**
     * 电影详情页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable Long id) {
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);

        for (Role role : movie.getRoles()
                ) {
            System.out.println(role.getName());

        }
        return "movie/detail";
    }

    /**
     * 跳转到新增页面add.html
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd() {
        return "movie/add";
    }

    /**
     * 执行保存操作，然后重定向到电影列表页index.html
     *
     * @param name
     * @param photo
     * @param createDate
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam String name, @RequestParam String photo, @RequestParam String createDate) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);

        movieRepository.save(movie);

        return "redirect:/movie/index";
    }

    /**
     * 跳转到编辑页面edit.html
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id) {

        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);
        return "/movie/edit";
    }

    /**
     * 执行保存操作，然后重定向到电影详情页detail.html
     *
     * @param id
     * @param name
     * @param photo
     * @param createDate
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Long id, @RequestParam String name, @RequestParam String photo, @RequestParam String createDate) {

        Movie movie = movieRepository.findOne(id);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);

        movieRepository.save(movie);

        return "redirect:/movie/detail/" + movie.getId();
    }


    /**
     * 删除
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/delect/{id}", method = RequestMethod.GET)
    public String delect(Model model, @PathVariable Long id) {

        movieRepository.delete(id);

        return "redirect:/movie/index";
    }

}
